package business.inhospitalqc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;

import static common.Login.login;
import static common.TestStep.requestAndRespondBody;


public class BatchCreate {
    Logger log = Logger.getLogger(BatchCreate.class);
    public static final String system = "env.properties";
//    TemplateResultList tmpResuList = new TemplateResultList();
    Data data = login();

 /**
  * @param i  api-sourcedata/qc/batch/batchCreate.json的datas
  * @param map  api-sourcedata/dmp/template/result/list返回的datas集里第一个的inpatientNo和templateId
//  * @param k  api-sourcedata/dmp/template/create的datas
  * @return java.lang.String
  * @Description: 发起质控指派/批量创建
  * @author gujinle
  * @date 2021-05-06 17:55
  */
    public String BatchCreate(int i,HashMap map) throws FileNotFoundException, ConfigurationException, YamlException {
//       HashMap map= tmpResuList.GetPatientList(j,k);
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject dataQueryWhereIn = new JSONObject();
        dataQueryWhereIn.put("inpatientNo", map.get("inpatientNo"));
        JSONObject obj = new JSONObject();
        obj.put("qcCheckOption", yaml.getDatas().get(i).get("qcCheckOption"));
        /*4.0是英文*/
        obj.put("inpatientNo", map.get("inpatientNo"));

        ArrayList arrayList = new ArrayList();
        arrayList.add(obj);

        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("types",  yaml.getDatas().get(i).get("types"));   //1=copd，2=aecopd 检测表，3=指标检测图
        json.put("templateId", map.get("templateId"));    ///api-sourcedata/dmp/template/create获取id
        json.put("resultType",  yaml.getDatas().get(i).get("resultType"));
        json.put("qcCheckOption",  yaml.getDatas().get(i).get("qcCheckOption"));    //质量管理措施项目（1-6级）
        json.put("diseaseCode",  yaml.getDatas().get(i).get("diseaseCode"));
        json.put("dataQueryWhereIn", dataQueryWhereIn.toJSONString());
        json.put("ids", arrayList.toString());

        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "post", yaml.getUri());
        String jsonStr = response.asString();
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath+  yaml.getUri();
        requestAndRespondBody(url,json,jsonStr);   //写进allure报告
        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject obj1 = jsonObject.getJSONObject("responseData");
        String batchId = obj1.getString("id");     //返回batchId
        return batchId;
    }


/**
*
* @Description: For 3.7.0SP3 version
 * @param i
 * @param map
* @return java.lang.String
* @author gujinle
* @date 2021-09-22 16:17
*/
    public String BatchCreate37(int i,HashMap map) throws FileNotFoundException, ConfigurationException, YamlException {
//       HashMap map= tmpResuList.GetPatientList(j,k);
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());


        JSONObject dataQueryWhereIn = new JSONObject();
        dataQueryWhereIn.put("住院流水号", map.get("inpatientNo"));
        JSONObject obj = new JSONObject();
        obj.put("qcCheckOption", yaml.getDatas().get(i).get("qcCheckOption"));
        /*3.7.0是中文*/
        obj.put("住院流水号", map.get("住院流水号"));
        /*4.0是英文*/
//        obj.put("inpatientNo", map.get("inpatientNo"));

        ArrayList arrayList = new ArrayList();
        arrayList.add(obj);

        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("types",  yaml.getDatas().get(i).get("types"));   //1=copd，2=aecopd 检测表，3=指标检测图
        json.put("templateId", map.get("templateId"));    ///api-sourcedata/dmp/template/create获取id
/*        json.put("resultType",  yaml.getDatas().get(i).get("resultType"));
        json.put("qcCheckOption",  yaml.getDatas().get(i).get("qcCheckOption"));    //质量管理措施项目（1-6级）
        json.put("diseaseCode",  yaml.getDatas().get(i).get("diseaseCode"));*/
        json.put("dataQueryWhereIn", dataQueryWhereIn.toJSONString());
//        json.put("ids", arrayList.toString());

        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "post", yaml.getUri());
        String jsonStr = response.asString();
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath+  yaml.getUri();
        requestAndRespondBody(url,json,jsonStr);   //写进allure报告
        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject obj1 = jsonObject.getJSONObject("responseData");
        String batchId = obj1.getString("id");     //返回batchId
        return batchId;
    }

 public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {
  BatchCreate bc=new BatchCreate();
//     String batchid=bc.BatchCreate(0,0,0);
//  System.out.println(batchid);

 }
}
